<?php

/**
 * This is the model class for table "{{order}}".
 *
 * The followings are the available columns in table '{{order}}':
 * @property integer $id
 * @property integer $amount
 * @property integer $user_id
 * @property string $create_time
 * @property string $update_time
 * @property integer $update_user_id
 * @property integer $status
 */
class BSMOrder extends CActiveRecord
{
    public static $STATUS = array('Booked', 'Paying', 'Saved', 'Cancel');
    /**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{order}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('create_time', 'required'),
			array('amount, user_id, update_user_id, status', 'numerical', 'integerOnly'=>true),
			array('update_time', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, amount, user_id, create_time, update_time, update_user_id, status', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
            'bookings'=>array(self::HAS_MANY, 'BSMBooking', 'order_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'amount' => 'Amount',
			'user_id' => 'User',
			'create_time' => 'Create Time',
			'update_time' => 'Update Time',
			'update_user_id' => 'Update User',
			'status' => 'Status',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('amount',$this->amount);
		$criteria->compare('user_id',$this->user_id);
		$criteria->compare('create_time',$this->create_time,true);
		$criteria->compare('update_time',$this->update_time,true);
		$criteria->compare('update_user_id',$this->update_user_id);
		$criteria->compare('status',$this->status);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * @return CDbConnection the database connection used for this class
	 */
	public function getDbConnection()
	{
		return Yii::app()->bookingdb;
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return BSMOrder the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

    protected function beforeValidate()
    {
        if ($this->isNewRecord) {
            $this->create_time = date('Y-m-d H:i:s');
            $user = yii::app()->user->id;
            $this->user_id = yii::app()->user->id;
        }

        return parent::beforeValidate();
    }

    protected function beforeSave() {
        if (parent::beforeSave()) {
            if (!$this->isNewRecord) {
                $this->update_time = date('Y-m-d H:i:s');
                $this->update_user_id = yii::app()->user->id;
            }
            
            return true;
        } else {
            return false;
        }
    }
    
    public function getList($userId = NULL)
    {
        $criteria = new CDbCriteria;

        if ($userId != NULL) {
            $criteria->addCondition("user_id = :user_id");
            $criteria->params[':user_id'] = $userId;
        }
        
        return self::model()->findAll($criteria);
    }
    
    public function getOrderStatus($status) {
        return self::$STATUS[$status - 1];
    }
}
